<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="hover">
      <el-button type="primary" @click="getParentCateList">添加分类</el-button>
      <tree-table
        :data="categoryList"
        :columns="this.columns"
        :selection-type="false"
        :expand-type="false"
        :show-index="true"
        index-text="#"
      >
        <template slot="isok" slot-scope="scope">
          <i
            class="el-icon-success"
            v-if="scope.row.cat_deleted === false"
            style="color: lightgreen"
          ></i>
          <i class="el-icon-error" v-else style="color: red"></i>
        </template>
        <template slot="sort" slot-scope="scope">
          <el-tag type="" v-if="scope.row.cat_level === 0" size="mini"
            >一级</el-tag
          >
          <el-tag
            type="success"
            v-else-if="scope.row.cat_level === 1"
            size="mini"
            >二级</el-tag
          >

          <el-tag type="warning" v-else size="mini">三级</el-tag>
        </template>
        <template slot="opt">
          <el-button type="primary" icon="el-icon-edit" size="mini"
            >编辑</el-button
          >
          <el-button type="danger" icon="el-icon-delete" size="mini"
            >删除</el-button
          >
        </template>
      </tree-table>
      <el-pagination
        @size-change="cateSizeChange"
        @current-change="cateCurrentChange"
        :current-page="this.queryInfo.pagenum"
        :page-size="this.queryInfo.pagesize"
        :page-sizes="[3, 5, 10, 15]"
        layout="total,sizes,prev, pager, next, jumper"
        :total="this.total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加分类对话框 -->
    <el-dialog title="添加分类" :visible.sync="addCateVisible" width="50%">
      <el-form ref="categoryRef" :model="addCateForm" label-width="70px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父类分类">
          <el-cascader
            :options="parentCateList"
            :props="cascaderProps"
            @change="parentCateChange()"
            v-model="selectedKeys"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateClosed">取 消</el-button>
        <el-button type="primary" @click="addCate()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      categoryList: [],
      addCateVisible: false,
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      total: 0,
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name',
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isok',
        },
        {
          label: '排序',
          type: 'template',
          template: 'sort',
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt',
        },
      ],
      parentCateList: [],
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        expandTrigger: 'hover',
        checkStrictly: '',
      },
      selectedKeys: [],
      addCateForm: {
        cat_pid: 0,
        cat_name: '',
        cat_level: 0,
      },
    }
  },
  created() {
    this.getCategories()
  },
  methods: {
    async getCategories() {
      const { data: res } = await this.$axios.get('categories', {
        params: this.queryInfo,
      })
      this.total = res.data.total
      this.categoryList = res.data.result
    },
    cateSizeChange(val) {
      this.queryInfo.pagesize = val
      this.getCategories()
    },
    cateCurrentChange(val) {
      this.queryInfo.pagenum = val
      this.getCategories()
    },
    async getParentCateList() {
      this.addCateVisible = true
      const { data: res } = await this.$axios.get('categories', {
        params: { type: 2 },
      })
      console.log(res.data)
      this.parentCateList = res.data
    },
    parentCateChange() {
      if (this.selectedKeys.length > 0) {
        this.addCateForm.cat_pid =
          this.selectedKeys[this.selectedKeys.length - 1]
        this.addCateForm.level = this.selectedKeys.length
        return
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.level = 0
      }
    },
    addCateClosed() {
      this.$refs.categoryRef.resetFields()
      this.selectedKeys = []
      this.addCateForm.cat_pid = 0
      this.addCateForm.cat_level = 0
      this.addCateVisible = false
    },
    addCate() {
      this.$refs.categoryRef.validate(async (valid) => {
        if (!valid) return
        const { data: res } = await this.$axios.post(
          'catagories',
          this.addCateForm
        )
        console.log(res)

        if (res.meta.status !== 201) {
          return this.$message.error('添加失败！')
        }
        this.$message.success('添加成功！')
        this.getCategories()
        this.addCateVisible = false
      })
    },
  },
}
</script>
<style lang="less" scoped>
.el-button {
  margin-bottom: 20px;
}
.el-cascader {
  width: 100%;
}
</style>